﻿Imports System.ComponentModel
Imports System.Management
Imports System
Imports System.Messaging

' Author : C.Dhakchina Moorthy (cdm@shss.co.in)
' Created Date : 02-May-2012
' Last Updated : 03-May-2012
Public Class frmDataCollector
    Dim o As DnBData = New DnBData
    Dim resDataTable As ResultDataSet.ResultDataTableDataTable
    Dim totalResultCount As Integer
    Dim recordsRetrieved As Integer
    Dim searchCriteria As String
    Dim currentZipcode As String
    Dim txtTotalRetrieved As Integer
    'Public NewRecordCounter As Integer


    Private Sub frmDataCollector_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            cboSearchBy.SelectedIndex = 0
            txtSearchText.Text = ""

            lblQCountStart.Text = GetQCount()
            DnBData.CheckIfQExists(My.Settings.ErrorQueueName)
            loadErrorQ()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click

        Try
            o.NextOffset = tbRowSearchStart.Text
            If txtSearchText.Text.Trim.Length = 0 And Not cboSearchBy.Text = "Zipcode" Then
                MsgBox("Please enter the search text!")
                Exit Sub
            End If
            Dim searchArgs As New SearchDataArguments
            With searchArgs
                .searchBy = cboSearchBy.Text
                .searchText = txtSearchText.Text
                .txtCount = txtCount.Value
                .maxRetrieveCount = txtCount.Value
            End With
            btnSearch.Enabled = False
            btnFetchNext.Enabled = False
            searchCriteria = cboSearchBy.Text


            ResultsBackgroundWorker.WorkerReportsProgress = True
            ResultsBackgroundWorker.RunWorkerAsync(searchArgs)


            'If PopuplateData() Then
            '    MsgBox("Search Completed!")
            'Else
            '    MsgBox("No results found!")
            'End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Function PopuplateData() As Boolean
        Try
            Dim resDataTable As New ResultDataSet.ResultDataTableDataTable
            Dim res As DnBDirect.AdvancedCompanySearchResponse = Nothing
            res = o.SearchData(cboSearchBy.Text, txtSearchText.Text, txtCount.Value)

            If Val(res.return.companies.hits) > 0 Then
                recordsRetrieved = 0
                Dim lRange As String
                While recordsRetrieved < res.return.companies.totalHits + 20
                    lRange = (o.CurrentOffset + 1) & " - " & o.CurrentOffset + Val(res.return.companies.hits)
                    ' lblResults.Text = "No. of results received: " & lRange
                    lblResults.Text = "No. of results received: " & recordsRetrieved
                    recordsRetrieved += res.return.companies.hits
                    res = o.SearchData(cboSearchBy.Text, txtSearchText.Text, txtCount.Value)
                    o.PopulateResultsDataTable(res, resDataTable)
                End While
                lblTotalResultsFound.Text = "Total results found: " & res.return.companies.totalHits
                gvResult.DataSource = resDataTable
                Return True
            End If
            Return False
        Catch ex As Exception
            Throw
        End Try
    End Function

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        If ResultsBackgroundWorker.IsBusy Then
            If ResultsBackgroundWorker.WorkerSupportsCancellation Then
                ResultsBackgroundWorker.CancelAsync()
            End If
        End If
        Me.Close()
    End Sub

    Private Sub btnFetchNext_Click(sender As System.Object, e As System.EventArgs) Handles btnFetchNext.Click
        Dim searchArgs As New SearchDataArguments
        With searchArgs
            .searchBy = cboSearchBy.Text
            .searchText = txtSearchText.Text
            .txtCount = txtCount.Value
            .maxRetrieveCount = txtCount.Value
        End With
        ResultsBackgroundWorker.RunWorkerAsync(searchArgs)
        'Try
        '    If PopuplateData() Then
        '        MsgBox("Search Completed!")
        '    Else
        '        MsgBox("No results found!")
        '    End If
        'Catch ex As Exception
        '    MsgBox(ex.Message)
        'End Try
    End Sub

    Private Sub ResultsBackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ResultsBackgroundWorker.DoWork
        Dim searchArgs As SearchDataArguments = e.Argument
        resDataTable = New ResultDataSet.ResultDataTableDataTable
        Dim res As DnBDirect.AdvancedCompanySearchResponse = Nothing
        If searchCriteria = "Zipcode" Then
            txtTotalRetrieved = 0
            'NewRecordCounter = 0
            Dim totalHitsCtr As Integer = 0
            Dim remainingHits As Integer = 0
            Dim zipCodeListDataset As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter
            Dim zipCodeList = From zCode In zipCodeListDataset.GetData
                              Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull)
                              Order By zCode.ZipCode

            If cbSort.Checked Then
                zipCodeList = zipCodeList.OrderByDescending(Function(x) x.ZipCode)
            End If

            For Each item In zipCodeList
                currentZipcode = item.ZipCode
                recordsRetrieved = 0
                'Dim zipInt As Integer = item.ZipCode.Split(" ")(1)
                With searchArgs
                    .searchText = item.ZipCode
                End With




                If Not item.IsOffsetNull Then
                    o.NextOffset = item.Offset
                    recordsRetrieved = item.Offset
                Else
                    o.NextOffset = 0
                End If
                res = o.SearchData(searchArgs.searchBy, searchArgs.searchText, 200)
                totalHitsCtr = res.return.companies.totalHits
                totalResultCount = totalHitsCtr
                txtTotalRetrieved += totalHitsCtr
                remainingHits = totalHitsCtr - res.return.companies.hits - If(item.IsOffsetNull, 0, item.Offset)
                If res.return.companies.hits > 0 Then
                    o.PopulateResultsDataTable(res, resDataTable)
                    While (remainingHits > 0 And remainingHits < 9800) Or res.return.companies.hits > 0
                        recordsRetrieved += res.return.companies.hits
                        res = o.SearchData(searchArgs.searchBy, searchArgs.searchText, 200)
                        o.PopulateResultsDataTable(res, resDataTable)
                        ResultsBackgroundWorker.ReportProgress(recordsRetrieved) 'res.return.companies.totalHits
                        remainingHits -= res.return.companies.hits
                    End While
                    recordsRetrieved += res.return.companies.hits
                    ResultsBackgroundWorker.ReportProgress(recordsRetrieved) 'res.return.companies.totalHits
                End If
            Next
            If resDataTable.Rows.Count > 0 Then
                e.Result = True
            Else
                e.Result = False
            End If
        Else
            res = o.SearchData(searchArgs.searchBy, searchArgs.searchText, searchArgs.txtCount)
            totalResultCount = res.return.companies.totalHits
            recordsRetrieved = Val(res.return.companies.hits)
            If Val(res.return.companies.hits) > 0 Then
                o.PopulateResultsDataTable(res, resDataTable)
                If Val(res.return.companies.hits) >= 200 Then

                    Dim lRange As String
                    While recordsRetrieved < searchArgs.maxRetrieveCount
                        lRange = (o.CurrentOffset + 1) & " - " & o.CurrentOffset + Val(res.return.companies.hits)
                        recordsRetrieved += res.return.companies.hits
                        res = o.SearchData(searchArgs.searchBy, searchArgs.searchText, searchArgs.txtCount)
                        o.PopulateResultsDataTable(res, resDataTable)
                        ResultsBackgroundWorker.ReportProgress(recordsRetrieved) 'res.return.companies.totalHits
                    End While
                    e.Result = True
                Else
                    e.Result = False
                End If
            Else
                e.Result = False
            End If
        End If
    End Sub



    Private Sub ResultsBackgroundWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles ResultsBackgroundWorker.ProgressChanged
        'ProgressBarFetchResult.Value = e.ProgressPercentage
        'recordsRetrieved = e.ProgressPercentage
        If cboSearchBy.Text = "Zipcode" Then
            txtSearchText.Text = currentZipcode
            txtCount.Value = totalResultCount
        Else
            txtTotalRetrieved = totalResultCount
        End If


        If totalResultCount > 0 Then

            ProgressBarFetchResult.Value = CInt((If(e.ProgressPercentage > txtCount.Value, txtCount.Value, e.ProgressPercentage) / txtCount.Value) * 100)
        End If
        lblNewRecordCounter.Text = "Total New Records: " & o.InsertedCount
        lblTotalUodatedRecords.Text = "Total Updated Records: " & o.UpdatedCount
        'lblResults.Text = "No. of results received: " & e.ProgressPercentage & "% of " & txtCount.Text
        lblResults.Text = "No. of results received: " & ProgressBarFetchResult.Value & "% of " & txtCount.Text
        lblTotalResultsFound.Text = "Total results found: " & txtTotalRetrieved
    End Sub

    Private Sub ResultsBackgroundWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles ResultsBackgroundWorker.RunWorkerCompleted
        If e.Cancelled Then
            MsgBox("Search cancelled!")
        Else
            lblResults.Text = "No. of results returned: " & recordsRetrieved
            gvResult.DataSource = resDataTable
            If e.Result Then
                MsgBox("Search Completed!")
            Else
                MsgBox("No results found!")
            End If
            'lblResults.Text = txtSearchText.Text
            btnSearch.Enabled = True
            btnFetchNext.Enabled = True
        End If
    End Sub

    Public Class SearchDataArguments
        Public searchBy As String
        Public searchText As String
        Public txtCount As Integer
        Public maxRetrieveCount As Integer
        Public dnbData As DnBData
        Public bgWorker As BackgroundWorker
        Public res As DnBDirect.AdvancedCompanySearchResponse
        Public sortDirection As Boolean
    End Class



    Private Sub btnSearch1_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch1.Click

        Try

            If tbZipcode1.Text.Trim.Length = 0 Then
                MsgBox("Please enter the search text for Search 1!")
                Exit Sub
            End If
            Dim dnbdata1 As New DnBData
            Dim searchArgs As New SearchDataArguments
            Dim res1 As New DnBDirect.AdvancedCompanySearchResponse
            With searchArgs
                .searchBy = "Zipcode"
                .searchText = tbZipcode1.Text
                .txtCount = 1
                .maxRetrieveCount = 1
                .dnbData = dnbdata1
                .bgWorker = BackgroundWorkerSearch1
                .res = res1
                .sortDirection = cbSortOrder1.Checked
            End With
            btnSearch1.Enabled = False

            BackgroundWorkerSearch1.WorkerReportsProgress = True
            BackgroundWorkerSearch1.RunWorkerAsync(searchArgs)

        Catch ex As Exception
            CommonClass.WriteToEventLog("frmDataCollector : " & ex.Message.ToString)
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BackgroundWorkerSearch1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerSearch1.DoWork
        e.Result = bgProcess1(e.Argument)
    End Sub

    Private Sub BackgroundWorkerSearch1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorkerSearch1.ProgressChanged
        Dim str As [String]() = DirectCast(e.UserState, [String]())

        lblNowSearching1.Text = "Now searching ZipCode : " & str(0)
        ProgressBarSearch1.Value = If(e.ProgressPercentage > 0, CInt((If(e.ProgressPercentage > CInt(str(2)), CInt(str(2)), e.ProgressPercentage) / CInt(str(2))) * 100), 0)

        lblResultsReturned1.Text = "No. of results received: " & ProgressBarSearch1.Value & "% of " & str(2)
        lblTotalFound1.Text += CInt(str(3))
        'GetQCount()
    End Sub

    Private Sub BackgroundWorkerSearch1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerSearch1.RunWorkerCompleted
        If e.Cancelled Then
            MsgBox("Search 1 cancelled!")
        Else
            If e.Result Then
                MsgBox("Search 1 Completed!")
            Else
                MsgBox("No results found for Search 1, try again after an hour")
            End If

            btnSearch1.Enabled = True

        End If
        'GetQCount()
    End Sub

    Private Sub btnStop1_Click(sender As System.Object, e As System.EventArgs) Handles btnStop1.Click
        If BackgroundWorkerSearch1.IsBusy Then
            If BackgroundWorkerSearch1.WorkerSupportsCancellation Then
                BackgroundWorkerSearch1.CancelAsync()
            End If
        End If
        btnSearch1.Enabled = True
        GetQCount()
    End Sub

    Function bgProcess1(vsearchArgsOrig As SearchDataArguments) As Boolean
        Dim extractResult As Boolean = False
        Dim runWhileTrue As Boolean = True
        Dim vsearchArgs As New SearchDataArguments
        Dim retryCtr As Integer = 0
        vsearchArgs = vsearchArgsOrig
        Do Until runWhileTrue = False
            Dim zipCodeListDataset As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter

            Try

                Dim vres As DnBDirect.AdvancedCompanySearchResponse = vsearchArgs.res
                Dim vtotalHitsCtr As Integer = 0
                Dim vremainingHits As Integer = 0
                Dim vcurZipcode As String = String.Empty

                Dim vrecordsRetrieved As Integer = 0

                Dim zipCodeList = From zCode In zipCodeListDataset.GetData
                                  Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
                                  zCode.ZipCode.ToString.StartsWith(vsearchArgs.searchText) And (DateDiff(DateInterval.Hour, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)
                                  Order By zCode.ZipCode

                If vsearchArgs.sortDirection = True Then
                    zipCodeList = zipCodeList.OrderByDescending(Function(x) x.ZipCode)
                End If


                vsearchArgs.dnbData.NextOffset = 0

                For Each item In zipCodeList
                    vrecordsRetrieved = 0
                    vremainingHits = 0
                    vtotalHitsCtr = 0
                    vcurZipcode = item.ZipCode

                    If Not item.IsOffsetNull Then
                        vsearchArgs.dnbData.NextOffset = item.Offset
                        vrecordsRetrieved = item.Offset
                    Else
                        vsearchArgs.dnbData.NextOffset = 0
                    End If
                    vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
                    'recheck if first 200 is blank
                    If vres.return.companies.hits = 0 Then
                        vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
                    End If
                    vtotalHitsCtr = vres.return.companies.totalHits
                    vremainingHits = vtotalHitsCtr - vres.return.companies.hits - If(item.IsOffsetNull, 0, item.Offset)
                    vrecordsRetrieved += vres.return.companies.hits + If(item.IsOffsetNull, 0, item.Offset)
                    vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
                    If Not vres.return.companies.hits < 200 Then

                        While (vremainingHits > 0 And vremainingHits < 9800)
                            vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
                            vrecordsRetrieved += vres.return.companies.hits
                            vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
                            vremainingHits -= vres.return.companies.hits
                        End While
                        vrecordsRetrieved += vres.return.companies.hits
                        vsearchArgs.bgWorker.ReportProgress(recordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
                    End If
                Next



                extractResult = True

            Catch ex As Exception
                CommonClass.WriteToEventLog("frmDataCollector - bgProcess : " & ex.Message.ToString)
                extractResult = False
            Finally
                Dim zipCodeListDatasetF As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter
                Dim zipCodeListFinished = From zCode In zipCodeListDatasetF.GetData
                                      Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
                                      zCode.ZipCode.ToString.StartsWith(vsearchArgs.searchText) And (DateDiff(DateInterval.Hour, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)

                If zipCodeListFinished.Count = 0 Then
                    runWhileTrue = False
                    CommonClass.WriteToEventLog("frmDataCollector no zip found - " & vsearchArgs.searchText & " zip", "DNB - ZIP Count")
                Else
                    runWhileTrue = True
                    retryCtr += 1
                    If retryCtr > 10 Then
                        runWhileTrue = False
                        extractResult = False
                    End If
                End If
            End Try

        Loop
        Return extractResult
    End Function
    'Function bgProcess2(vsearchArgsOrig As SearchDataArguments) As Boolean
    '    Dim extractResult As Boolean = False
    '    Dim runWhileTrue As Boolean = True
    '    Dim vsearchArgs As New SearchDataArguments
    '    vsearchArgs = vsearchArgsOrig
    '    Do Until runWhileTrue = False
    '        Dim zipCodeListDataset As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter

    '        Try

    '            Dim vres As DnBDirect.AdvancedCompanySearchResponse = vsearchArgs.res
    '            Dim vtotalHitsCtr As Integer = 0
    '            Dim vremainingHits As Integer = 0
    '            Dim vcurZipcode As String = String.Empty

    '            Dim vrecordsRetrieved As Integer = 0

    '            Dim zipCodeList = From zCode In zipCodeListDataset.GetData
    '                              Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
    '                              zCode.ZipCode.ToString.StartsWith(vsearchArgs.searchText) And (DateDiff(DateInterval.Day, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)
    '                              Order By zCode.ZipCode

    '            If vsearchArgs.sortDirection = True Then
    '                zipCodeList = zipCodeList.OrderByDescending(Function(x) x.ZipCode)
    '            End If
    '            vsearchArgs.dnbData.NextOffset = 0

    '            For Each item In zipCodeList
    '                vrecordsRetrieved = 0
    '                vremainingHits = 0
    '                vtotalHitsCtr = 0
    '                vcurZipcode = item.ZipCode

    '                If Not item.IsOffsetNull Then
    '                    vsearchArgs.dnbData.NextOffset = item.Offset
    '                    vrecordsRetrieved = item.Offset
    '                Else
    '                    vsearchArgs.dnbData.NextOffset = 0
    '                End If
    '                vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
    '                vtotalHitsCtr = vres.return.companies.totalHits
    '                vremainingHits = vtotalHitsCtr - vres.return.companies.hits - If(item.IsOffsetNull, 0, item.Offset)
    '                vrecordsRetrieved += vres.return.companies.hits + If(item.IsOffsetNull, 0, item.Offset)
    '                vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
    '                If Not vres.return.companies.hits < 200 Then

    '                    While (vremainingHits > 0 And vremainingHits < 9800)
    '                        vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
    '                        vrecordsRetrieved += vres.return.companies.hits
    '                        vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
    '                        vremainingHits -= vres.return.companies.hits
    '                    End While
    '                    vrecordsRetrieved += vres.return.companies.hits
    '                    vsearchArgs.bgWorker.ReportProgress(recordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
    '                End If
    '            Next



    '            extractResult = True

    '        Catch ex As Exception
    '            CommonClass.WriteToEventLog("frmDataCollector - bgProcess : " & ex.Message.ToString)
    '            extractResult = False
    '        Finally
    '            Dim zipCodeListDatasetF As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter
    '            Dim zipCodeListFinished = From zCode In zipCodeListDatasetF.GetData
    '                                  Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
    '                                  zCode.ZipCode.ToString.StartsWith(vsearchArgs.searchText) And (DateDiff(DateInterval.Hour, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)

    '            If zipCodeListFinished.Count = 0 Then
    '                runWhileTrue = False
    '                CommonClass.WriteToEventLog("frmDataCollector no zip found - " & vsearchArgs.searchText & " zip", "DNB - ZIP Count")
    '            Else
    '                runWhileTrue = True
    '            End If
    '        End Try

    '    Loop
    '    Return extractResult

    'End Function

    'Function bgProcess3(vsearchArgsOrig As SearchDataArguments) As Boolean
    '    Dim extractResult As Boolean = False
    '    Dim runWhileTrue As Boolean = True
    '    Dim vsearchArgs As New SearchDataArguments
    '    vsearchArgs = vsearchArgsOrig
    '    Do Until runWhileTrue = False
    '        Dim zipCodeListDataset As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter

    '        Try

    '            Dim vres As DnBDirect.AdvancedCompanySearchResponse = vsearchArgs.res
    '            Dim vtotalHitsCtr As Integer = 0
    '            Dim vremainingHits As Integer = 0
    '            Dim vcurZipcode As String = String.Empty

    '            Dim vrecordsRetrieved As Integer = 0

    '            Dim zipCodeList = From zCode In zipCodeListDataset.GetData
    '                              Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
    '                              zCode.ZipCode.ToString.StartsWith(vsearchArgs.searchText) And (DateDiff(DateInterval.Day, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)
    '                              Order By zCode.ZipCode

    '            If vsearchArgs.sortDirection = True Then
    '                zipCodeList = zipCodeList.OrderByDescending(Function(x) x.ZipCode)
    '            End If
    '            vsearchArgs.dnbData.NextOffset = 0

    '            For Each item In zipCodeList
    '                vrecordsRetrieved = 0
    '                vremainingHits = 0
    '                vtotalHitsCtr = 0
    '                vcurZipcode = item.ZipCode

    '                If Not item.IsOffsetNull Then
    '                    vsearchArgs.dnbData.NextOffset = item.Offset
    '                    vrecordsRetrieved = item.Offset
    '                Else
    '                    vsearchArgs.dnbData.NextOffset = 0
    '                End If
    '                vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
    '                vtotalHitsCtr = vres.return.companies.totalHits
    '                vremainingHits = vtotalHitsCtr - vres.return.companies.hits - If(item.IsOffsetNull, 0, item.Offset)
    '                vrecordsRetrieved += vres.return.companies.hits + If(item.IsOffsetNull, 0, item.Offset)
    '                vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
    '                If Not vres.return.companies.hits < 200 Then

    '                    While (vremainingHits > 0 And vremainingHits < 9800)
    '                        vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
    '                        vrecordsRetrieved += vres.return.companies.hits
    '                        vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
    '                        vremainingHits -= vres.return.companies.hits
    '                    End While
    '                    vrecordsRetrieved += vres.return.companies.hits
    '                    vsearchArgs.bgWorker.ReportProgress(recordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
    '                End If
    '            Next



    '            extractResult = True

    '        Catch ex As Exception
    '            CommonClass.WriteToEventLog("frmDataCollector - bgProcess : " & ex.Message.ToString)
    '            extractResult = False
    '        Finally
    '            Dim zipCodeListDatasetF As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter
    '            Dim zipCodeListFinished = From zCode In zipCodeListDatasetF.GetData
    '                                  Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
    '                                  zCode.ZipCode.ToString.StartsWith(vsearchArgs.searchText) And (DateDiff(DateInterval.Hour, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)

    '            If zipCodeListFinished.Count = 0 Then
    '                runWhileTrue = False
    '                CommonClass.WriteToEventLog("frmDataCollector no zip found - " & vsearchArgs.searchText & " zip", "DNB - ZIP Count")
    '            Else
    '                runWhileTrue = True
    '            End If
    '        End Try

    '    Loop
    '    Return extractResult

    'End Function



    Private Sub btnSearch2_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch2.Click

        Try

            If tbZipcode2.Text.Trim.Length = 0 Then
                MsgBox("Please enter the search text for Search 2!")
                Exit Sub
            End If
            Dim dnbdata2 As New DnBData
            Dim searchArgs As New SearchDataArguments
            Dim res2 As New DnBDirect.AdvancedCompanySearchResponse
            With searchArgs
                .searchBy = "Zipcode"
                .searchText = tbZipcode2.Text
                .txtCount = 1
                .maxRetrieveCount = 1
                .dnbData = dnbdata2
                .bgWorker = BackgroundWorkerSearch2
                .res = res2
                .sortDirection = cbSortOrder2.Checked
            End With
            btnSearch2.Enabled = False

            BackgroundWorkerSearch2.WorkerReportsProgress = True
            BackgroundWorkerSearch2.RunWorkerAsync(searchArgs)

        Catch ex As Exception
            CommonClass.WriteToEventLog("frmDataCollector : " & ex.Message.ToString)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnStop2_Click(sender As System.Object, e As System.EventArgs) Handles btnStop2.Click
        If BackgroundWorkerSearch2.IsBusy Then
            If BackgroundWorkerSearch2.WorkerSupportsCancellation Then
                BackgroundWorkerSearch2.CancelAsync()
            End If
        End If
        btnSearch2.Enabled = True
        GetQCount()

    End Sub

    Private Sub BackgroundWorkerSearch2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerSearch2.DoWork
        e.Result = bgProcess1(e.Argument)
    End Sub

    Private Sub BackgroundWorkerSearch2_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorkerSearch2.ProgressChanged
        Dim str As [String]() = DirectCast(e.UserState, [String]())

        lblNowSearching2.Text = "Now searching ZipCode : " & str(0)
        ProgressBarSearch2.Value = If(e.ProgressPercentage > 0, CInt((If(e.ProgressPercentage > CInt(str(2)), CInt(str(2)), e.ProgressPercentage) / CInt(str(2))) * 100), 0)
        lblResultsReturned2.Text = "No. of results received: " & ProgressBarSearch2.Value & "% of " & str(2)
        lblTotalFound2.Text += CInt(str(3))
        'GetQCount()
    End Sub

    Private Sub BackgroundWorkerSearch2_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerSearch2.RunWorkerCompleted
        If e.Cancelled Then
            MsgBox("Search 2 cancelled!")
        Else
            If e.Result Then
                MsgBox("Search 2 Completed!")
            Else
                MsgBox("No results found for Search2, try again after an hour")
            End If

            btnSearch2.Enabled = True

        End If
        GetQCount()
    End Sub

    Private Function GetQCount() As Integer
        DnBData.CheckIfQExists(My.Settings.QueueName)
        Dim dnbQ As New MessageQueue(My.Settings.QueueName)
        Dim x = dnbQ.GetMessageEnumerator2()
        Dim icount As Integer = 0
        While x.MoveNext()
            icount += 1
        End While
        lblQCount.Text = icount
        Return icount
    End Function

    Private Sub loadErrorQ()
        dgvErrorQ.DataSource = Nothing
        Dim messageTable As New DataTable()


        messageTable.Columns.Add("CompanyId")
        messageTable.Columns.Add("CompanyName")
        messageTable.Columns.Add("Address1")
        messageTable.Columns.Add("City")
        messageTable.Columns.Add("StateOrProvince")
        messageTable.Columns.Add("PostalCode")
        messageTable.Columns.Add("Phone")
        messageTable.Columns.Add("offset")
        messageTable.Columns.Add("LocationType")
        messageTable.Columns.Add("Duns")
        messageTable.Columns.Add("ParentDuns")
        messageTable.Columns.Add("UltimateParentDuns")
        messageTable.Columns.Add("Sales")
        messageTable.Columns.Add("DBA")
        messageTable.Columns.Add("CountryID")
        messageTable.Columns.Add("primaryURLs")
        messageTable.Columns.Add("Description")
        messageTable.Columns.Add("CompanyType")
        messageTable.Columns.Add("industryName")
        messageTable.Columns.Add("industryDescription")
        messageTable.Columns.Add("lineOfBusiness")
        messageTable.Columns.Add("primaryDnBIC")
        messageTable.Columns.Add("primaryNAICS")
        messageTable.Columns.Add("primaryUSSIC")
        messageTable.Columns.Add("naicsCodes")
        messageTable.Columns.Add("Longitude")
        messageTable.Columns.Add("Latitutude")
        messageTable.Columns.Add("WomenOwned")
        messageTable.Columns.Add("MinorityOwned")
        messageTable.Columns.Add("ownedOrLeased")
        messageTable.Columns.Add("industryCode")
        messageTable.Columns.Add("relatedIndustries")
        messageTable.Columns.Add("FastFacts")
        messageTable.Columns.Add("CompanyKeyword")
        messageTable.Columns.Add("LastUpdated")

        Dim errorq As New MessageQueue(My.Settings.ErrorQueueName)
        Dim messages() As System.Messaging.Message
        messages = errorq.GetAllMessages()
        errorq.Formatter = New XmlMessageFormatter(New Type() {GetType(vDNBData)})
        Dim index As Integer
        Dim am As System.Messaging.Message

        For index = 0 To messages.Length - 1
            messages(index).Formatter = errorq.Formatter
            am = messages(index)
            With am.Body
                messageTable.Rows.Add(.companyid, _
                                       .CompanyName, _
                                       .Address1, _
                                       .City, _
                                       .StateOrProvince, _
                .PostalCode, _
                .Phone, _
                .offset, _
                .LocationType, _
                .Duns, _
                .ParentDuns, _
                .UltimateParentDuns, _
                .Sales, _
                .DBA, _
                .CountryID, _
                .primaryURLs, _
                .Description, _
                .CompanyType, _
                .industryName, _
                .industryDescription, _
                .lineOfBusiness, _
                .primaryDnBIC, _
                .primaryNAICS, _
                .primaryUSSIC, _
                .naicsCodes, _
                .Longitude, _
                .Latitutude, _
                .WomenOwned, _
                .MinorityOwned, _
                .ownedOrLeased, _
                .industryCode, _
                .relatedIndustries, _
                .FastFacts, _
                .CompanyKeyword, _
                .LastUpdated)
            End With

        Next

        dgvErrorQ.DataSource = messageTable





    End Sub

    Private Sub btnRefresh_Click(sender As System.Object, e As System.EventArgs) Handles btnRefresh.Click
        loadErrorQ()
    End Sub

    Private Sub btnReProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnReProcess.Click
        Dim errorq As New MessageQueue(My.Settings.ErrorQueueName)
        Dim dnbQ As New MessageQueue(My.Settings.QueueName)
        errorq.Formatter = New XmlMessageFormatter(New Type() {GetType(vDNBData)})
        Dim msg As New Message
        For i = 0 To errorq.GetAllMessages.Count - 1
            msg = errorq.Receive()
            dnbQ.Send(msg.Body)
        Next
        'Static enumMessage As MessageEnumerator = errorq.GetMessageEnumerator2()

        'For Each msg As Message In enumMessage
        '    dnbQ.Send(msg.Body)

        'Next()

        '    AddHandler errorq.ReceiveCompleted, AddressOf MyReceiveCompleted

        '    ' Begin the asynchronous receive operation.
        '    errorq.BeginReceive(TimeSpan.FromSeconds(10.0))
        dnbQ.Close()
        errorq.Close()
        loadErrorQ()
    End Sub

    'Private Sub MyReceiveCompleted(ByVal source As Object, ByVal asyncResult As ReceiveCompletedEventArgs)
    '    ' Connect to the queue.
    '    Dim dnbQ As New MessageQueue(My.Settings.QueueName)



    '    Dim errorq As MessageQueue = CType(source, MessageQueue)
    '    errorq.Formatter = New XmlMessageFormatter(New Type() {GetType(vDNBData)})
    '    Dim _dnbdata As New vDNBData
    '    ' End the asynchronous receive operation.
    '    Dim msg As Message = errorq.EndReceive(asyncResult.AsyncResult)
    '    _dnbdata = msg.Body
    '    dnbQ.Send(_dnbdata)

    'End Sub

    Private Sub btnSearch3_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch3.Click

        Try

            If tbZipcode3.Text.Trim.Length = 0 Then
                MsgBox("Please enter the search text for Search 3!")
                Exit Sub
            End If
            Dim dnbdata3 As New DnBData
            Dim searchArgs As New SearchDataArguments
            Dim res3 As New DnBDirect.AdvancedCompanySearchResponse
            With searchArgs
                .searchBy = "Zipcode"
                .searchText = tbZipcode3.Text
                .txtCount = 1
                .maxRetrieveCount = 1
                .dnbData = dnbdata3
                .bgWorker = BackgroundWorkerSearch3
                .res = res3
                .sortDirection = cbSortOrder1.Checked
            End With
            btnSearch3.Enabled = False

            BackgroundWorkerSearch3.WorkerReportsProgress = True
            BackgroundWorkerSearch3.RunWorkerAsync(searchArgs)

        Catch ex As Exception
            CommonClass.WriteToEventLog("frmDataCollector : " & ex.Message.ToString)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnStop3_Click(sender As System.Object, e As System.EventArgs) Handles btnStop3.Click
        If BackgroundWorkerSearch3.IsBusy Then
            If BackgroundWorkerSearch3.WorkerSupportsCancellation Then
                BackgroundWorkerSearch3.CancelAsync()
            End If
        End If
        btnSearch3.Enabled = True
        GetQCount()
    End Sub

    Private Sub BackgroundWorkerSearch3_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerSearch3.DoWork
        e.Result = bgProcess1(e.Argument)
    End Sub

    Private Sub BackgroundWorkerSearch3_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorkerSearch3.ProgressChanged

        Dim str As [String]() = DirectCast(e.UserState, [String]())

        lblNowSearching3.Text = "Now searching ZipCode : " & str(0)
        ProgressBarSearch3.Value = If(e.ProgressPercentage > 0, CInt((If(e.ProgressPercentage > CInt(str(2)), CInt(str(2)), e.ProgressPercentage) / CInt(str(2))) * 100), 0)
        lblResultsReturned3.Text = "No. of results received: " & ProgressBarSearch3.Value & "% of " & str(2)
        lblTotalFound3.Text += CInt(str(3))
        'GetQCount()
    End Sub

    Private Sub BackgroundWorkerSearch3_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerSearch3.RunWorkerCompleted
        If e.Cancelled Then
            MsgBox("Search 3 cancelled!")
        Else
            If e.Result Then
                MsgBox("Search 3 Completed!")
            Else
                MsgBox("No results found for Search 3, try again after an hour")
            End If

            btnSearch3.Enabled = True

        End If
        GetQCount()
    End Sub

    Private Sub btnIncrementSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnIncrementSearch.Click
        Try

            If tbZipIncrementStart.Text.Trim.Length = 0 Then
                MsgBox("Please enter the search text for Search!")
                Exit Sub
            End If
            Dim dnbdataI As New DnBData
            Dim searchArgs As New SearchDataArguments
            Dim res1 As New DnBDirect.AdvancedCompanySearchResponse
            With searchArgs
                .searchBy = "Zipcode"
                .searchText = tbZipIncrementStart.Text
                .txtCount = 1
                .maxRetrieveCount = 1
                .dnbData = dnbdataI
                .bgWorker = BackgroundWorkerIncrement
                .res = res1
                .sortDirection = cbIncrementDescAsc.Checked
            End With
            btnIncrementSearch.Enabled = False

            BackgroundWorkerIncrement.WorkerReportsProgress = True
            BackgroundWorkerIncrement.RunWorkerAsync(searchArgs)

        Catch ex As Exception
            CommonClass.WriteToEventLog("frmDataCollector : " & ex.Message.ToString)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BackgroundWorkerIncrement_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerIncrement.DoWork
        e.Result = bgProcessIncrement(e.Argument)
    End Sub

    Private Sub BackgroundWorkerIncrement_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorkerIncrement.ProgressChanged

        Dim str As [String]() = DirectCast(e.UserState, [String]())

        lblIncrementCurrentZip.Text = "Now searching ZipCode : " & str(0)
        ProgressBarSearchIncrement.Value = If(e.ProgressPercentage > 0, CInt((If(e.ProgressPercentage > CInt(str(2)), CInt(str(2)), e.ProgressPercentage) / CInt(str(2))) * 100), 0)
        lblIncrementResultsReturned.Text = "No. of results received: " & ProgressBarSearchIncrement.Value & "% of " & str(2)
        lblIncrementTotalFound.Text += CInt(str(3))
        'GetQCount()
    End Sub

    Private Sub BackgroundWorkerIncrement_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerIncrement.RunWorkerCompleted
        If e.Cancelled Then
            MsgBox("Search 3 cancelled!")
        Else
            If e.Result Then
                MsgBox("Search 3 Completed!")
            Else
                MsgBox("No results found for Search, try again after an hour")
            End If

            btnIncrementSearch.Enabled = True

        End If
        GetQCount()
    End Sub

    Private Function bgProcessIncrement(vsearchArgsOrig As SearchDataArguments) As Boolean
        Dim extractResult As Boolean = False
        Dim runWhileTrue As Boolean = True
        Dim vsearchArgs As New SearchDataArguments
        Dim retryCtr As Integer = 0
        vsearchArgs = vsearchArgsOrig
        Do Until runWhileTrue = False
            Dim zipCodeListDataset As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter
            vsearchArgs = vsearchArgsOrig
            Try
                While If(cbIncrementDescAsc.Checked = False, vsearchArgs.searchText < 100000, vsearchArgs.searchText > 499)

                    Dim vres As DnBDirect.AdvancedCompanySearchResponse = vsearchArgs.res
                    Dim vtotalHitsCtr As Integer = 0
                    Dim vremainingHits As Integer = 0
                    Dim vcurZipcode As String = String.Empty

                    Dim vrecordsRetrieved As Integer = 0

                    Dim zipCodeList = From zCode In zipCodeListDataset.GetData
                                      Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
                                      zCode.ZipCode = vsearchArgs.searchText And (DateDiff(DateInterval.Hour, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)


                    If vsearchArgs.sortDirection = True Then
                        zipCodeList = zipCodeList.OrderByDescending(Function(x) x.ZipCode)
                    End If


                    vsearchArgs.dnbData.NextOffset = 0

                    If zipCodeList.Count > 0 Then

                        For Each item In zipCodeList
                            vrecordsRetrieved = 0
                            vremainingHits = 0
                            vtotalHitsCtr = 0
                            vcurZipcode = item.ZipCode

                            If Not item.IsOffsetNull Then
                                vsearchArgs.dnbData.NextOffset = item.Offset
                                vrecordsRetrieved = item.Offset
                            Else
                                vsearchArgs.dnbData.NextOffset = 0
                            End If
                            vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
                            vtotalHitsCtr = vres.return.companies.totalHits
                            vremainingHits = vtotalHitsCtr - vres.return.companies.hits - If(item.IsOffsetNull, 0, item.Offset)
                            vrecordsRetrieved += vres.return.companies.hits + If(item.IsOffsetNull, 0, item.Offset)
                            vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
                            If Not vres.return.companies.hits < 200 Then

                                While (vremainingHits > 0 And vremainingHits < 9800)
                                    vres = vsearchArgs.dnbData.SearchData(vsearchArgs.searchBy, vcurZipcode, 200)
                                    vrecordsRetrieved += vres.return.companies.hits
                                    vsearchArgs.bgWorker.ReportProgress(vrecordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
                                    vremainingHits -= vres.return.companies.hits
                                End While
                                vrecordsRetrieved += vres.return.companies.hits
                                vsearchArgs.bgWorker.ReportProgress(recordsRetrieved, New [String]() {vcurZipcode, vremainingHits, vtotalHitsCtr, vres.return.companies.hits})
                            End If
                        Next
                    Else
                        vsearchArgs.bgWorker.ReportProgress(0, New [String]() {vsearchArgs.searchText, 0, 0, 0})
                    End If
                    If cbIncrementDescAsc.Checked = False Then
                        vsearchArgs.searchText += 1
                    Else
                        vsearchArgs.searchText -= 1
                    End If
                End While
                extractResult = True

            Catch ex As Exception
                CommonClass.WriteToEventLog("frmDataCollector - bgProcess : " & ex.Message.ToString)
                extractResult = False
            Finally
                Dim zipCodeListDatasetF As New ZipCodeDataSetTableAdapters.ZipTableTableAdapter
                Dim zipCodeListFinished = From zCode In zipCodeListDatasetF.GetData
                                      Where If(Not zCode.IsOffsetNull And Not zCode.IsTotalHitsNull, ((zCode.Offset < zCode.TotalHits) Or zCode.Offset = 0) And zCode.Offset < 9800, zCode.IsOffsetNull) AndAlso
                                      (DateDiff(DateInterval.Hour, zCode.ExtractDate, Date.Now) > 1 Or zCode.IsExtractDateNull)

                If zipCodeListFinished.Count = 0 Then
                    runWhileTrue = False
                    CommonClass.WriteToEventLog("frmDataCollector no zip found - " & vsearchArgs.searchText & " zip", "DNB - ZIP Count")
                Else
                    runWhileTrue = True
                    retryCtr += 1
                    If retryCtr > 10 Then
                        runWhileTrue = False
                        extractResult = False
                    End If
                End If
            End Try

        Loop
        Return extractResult
    End Function

    Private Sub btnIncrementStop_Click(sender As System.Object, e As System.EventArgs) Handles btnIncrementStop.Click
        If BackgroundWorkerIncrement.IsBusy Then
            If BackgroundWorkerIncrement.WorkerSupportsCancellation Then
                BackgroundWorkerIncrement.CancelAsync()
            End If
        End If
        btnIncrementSearch.Enabled = True
        GetQCount()
    End Sub
End Class
